﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Product;

namespace BrokerBase.Lib.Policy
{
    public class SchemePeriodActing:ISchemePeriodActing
    {
        private int period;
        private int age;
        private int holderAge;
        private ISchemeActing schemeActing;
        private IPolicyPeriodActing[] ppas;
        private bool isCalc;

        public SchemePeriodActing(ISchemeActing schemeActing,int period,int age)
        {
            InsurantStates = new List<string>();
            HolderStates = new List<string>();

            this.schemeActing = schemeActing;

            this.period = period;
            this.age = age;
            this.holderAge = period + schemeActing.Scheme.HolderAge;

            int policieCount=schemeActing.Scheme.Policies.Count;

            ppas = new IPolicyPeriodActing[policieCount];

            for (int i = 0; i < policieCount; i++)
            {
                PolicyPeriodActings[i] = new PolicyPeriodActing(this, schemeActing.PolicyActings[i]);
                schemeActing.PolicyActings[i].PeriodActings[Period] = PolicyPeriodActings[i];
            }

        }

        public ISchemeActing SchemeActing 
        {
            get { return schemeActing; }
        }

        public int Period { get { return period; } }

        public int Age { get { return age; } }

        public int HolderAge { get { return holderAge; } }

        public int Year { get { return period + this.schemeActing.BeginYear; } }

        public double Amount
        {
            get
            {
                return PolicyPeriodActings.Where(p => p.PolicyActing.Policy.Product.UnitType == UnitType.Amt).Sum(q => q.Amount);
            }
        }

        public double　Fee
        {
            get
            {
                return PolicyPeriodActings.Sum(p => p.Fee);
            }
        }

        public double TotalFee
        {
            get
            {
                double tf = 0;
                for (int i = 0; i <= period; i++)
                {
                    tf += schemeActing.PeriodActings[i].Fee;
                }
                return tf;
            }
        }

        public double Value
        {
            get
            {
                return PolicyPeriodActings.Sum(p => p.Value);
            }
        }

        public double TotalValue 
        {
            get
            {
                return SchemeActing.PeriodActings.Where(p => p.Period <= Period).Sum(q => q.Value);
            }
        }

        public IList<string> InsurantStates { get; set; }

        public IList<string> HolderStates { get; set; }

        public IPolicyPeriodActing BasicPolicyPeriodActing { get { return ppas[0]; } }

        public IPolicyPeriodActing[] PolicyPeriodActings { get { return ppas; } }

        public IPolicyPeriodActing GetPolicyPeriodActiing(string name)
        {
            return PolicyPeriodActings.FirstOrDefault(p => p.PolicyActing.Policy.Product.Name == name);
        }

        public IPolicyPeriodActing GetBasicPolicyPeriodActing()
        {
            return PolicyPeriodActings.FirstOrDefault(p => p.PolicyActing.Policy.Product.ProductType == ProductType.Basic);
        }

        public string State 
        {
            get
            {
                string result = string.Empty;

                for (int i = 0; i < PolicyPeriodActings.Count();i++ )
                {
                    IPolicyPeriodActing ppa = PolicyPeriodActings[i];

                    string s = null;
                    if (ppa.IsCurrentFinish)
                    {
                        if (i == 0)
                            s = "主险合同终止";
                        else
                            s = string.Format("第[{0}个附加险合同终止",i);
                    }

                    result += string.IsNullOrEmpty(result) ? s : ";" + s;
                }

                return result;
            }
        }

        public void Calc()
        {
            isCalc = true;
            while (isCalc)
            {
                isCalc = false;
                foreach (var ppa in ppas)
                {
                    ppa.Calc();
                }
            }
        }



        public void AddInsurantStates(string state)
        {
            if (!this.InsurantStates.Contains(state))
            {
                this.InsurantStates.Add(state);
                this.isCalc = true;
            }
        }

        public void AddHolderStates(string state)
        {
            if (!this.HolderStates.Contains(state))
            {
                this.HolderStates.Add(state);
                this.isCalc = true;
            }
        }
    }
}
